package com.ruix.app.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.IMulItemViewType;
import com.ruix.app.adapter.superadapter.SuperAdapter;
import com.ruix.app.adapter.superadapter.internal.SuperViewHolder;
import com.ruix.app.manager.UserManager;
import com.ruix.app.model.Comment;
import com.ruix.app.model.PostsCard;
import com.ruix.app.model.PostsType;
import com.ruix.app.ui.app.PhotoViewActivity;
import com.ruix.app.util.GlideUitl;
import com.ruix.app.util.PhotoView;
import com.ruix.app.widget.PostContentView;
import com.ruix.app.widget.linearlistview.LinearListView;
import com.ruix.app.widget.praise.PraiseTextView;

import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPreview;

/**
 * Created by hadexs on 2017/4/22.
 */

public class PracticalCommuniAdapter extends SuperAdapter<PostsCard> {


    private CommentAdapter commentAdapter;
    private PracticalCommuActionListenler listenler;
    private String userId;
    private Activity activity;
    private boolean canShare;
    private boolean hasMoreAction;
    private boolean isFriendCircle;
//    private boolean canDelete;
    private boolean prohibited;
    private int[] viewIds ={R.id.iv_pic_one,R.id.iv_pic_two,
            R.id.iv_pic_three,R.id.iv_pic_four,
            R.id.iv_pic_five,R.id.iv_pic_six,
            R.id.iv_pic_seven,R.id.iv_pic_eight,R.id.iv_pic_nine};

    public PracticalCommuniAdapter(Context context, List<PostsCard> items, IMulItemViewType<PostsCard> mulItemViewType) {
        super(context, items, mulItemViewType);
        userId = UserManager.getCurrentUser().getID();
//        canDelete = true;
        hasMoreAction = true;
        commentAdapter = new CommentAdapter(context);
    }

    @Override
    public void onBind(final SuperViewHolder holder, int viewType, final int position, final PostsCard item) {
        holder.setSimpleDraweeURI(R.id.iv_auth_avatar, item.getAvatarUrl());
        holder.setText(R.id.tv_auth_name, item.getUserName());
        holder.setText(R.id.tv_create_time, item.getCreatedTimeDisplay());

        holder.setOnClickListener(R.id.iv_auth_avatar, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    listenler.clickAvatar(item, position);
                }

            }
        });

        //角色
        String roleValue = item.authorRoleValue(isFriendCircle);
        if (roleValue != null) {
            //
            holder.setText(R.id.auther_role, roleValue);
            holder.setVisibility(R.id.auther_role, View.VISIBLE);
        } else {
            holder.setVisibility(R.id.auther_role, View.GONE);
        }


        if (item.isHasPraisedByUser()) {
            holder.setImageViewSrc(R.id.btn_good_up, R.drawable.like_praised);
        } else {
            holder.setImageViewSrc(R.id.btn_good_up, R.drawable.like_normal);
        }

        // 点赞
        ArrayList list = item.getLikes();
        if (list != null && list.size() > 0) {
            PraiseTextView widget = holder.getView(R.id.tw_praise_widget);
            widget.setData(list);
            holder.setVisibility(R.id.tw_praise_widget, View.VISIBLE);
        } else {
            holder.setVisibility(R.id.tw_praise_widget, View.GONE);
        }

        //点赞
        holder.setOnClickListener(R.id.btn_good_up, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    if (item.isHasPraisedByUser()) {
                        listenler.unpraisePosts(item, position);
                    } else {
                        listenler.praisePosts(item, position);
                    }
                }
            }
        });

        //评论
        holder.setOnClickListener(R.id.btn_comment_posts, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    listenler.commentPosts(item, position);
                }
            }
        });
//        if (prohibited) { //禁言不能评论
//            holder.setVisibility(R.id.btn_comment_posts, View.GONE);
//        } else {
//            holder.setVisibility(R.id.btn_share_posts, View.VISIBLE);
//        }


        //分享
        holder.setOnClickListener(R.id.btn_share_posts, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    listenler.sharePosts(item);
                }
            }
        });
        if (canShare) {
            holder.setVisibility(R.id.btn_share_posts, View.VISIBLE);
        } else {
            holder.setVisibility(R.id.btn_share_posts, View.GONE);
        }

        //删除
        if (item.getUid().equals(userId)) {
            holder.setVisibility(R.id.btn_detele_posts, View.VISIBLE);
            holder.setOnClickListener(R.id.btn_detele_posts, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listenler != null) {
                        listenler.deletePosts(item, position);
                    }
                }
            });
        } else {
            holder.setVisibility(R.id.btn_detele_posts, View.GONE);
        }

//        if (!canDelete) {
//            holder.setVisibility(R.id.btn_detele_posts, View.GONE);
//        }

        //更多
        holder.setOnClickListener(R.id.btn_more_action, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listenler != null) {
                    listenler.moreActionPosts(item);
                }
            }
        });
        if (!hasMoreAction) {
            holder.setVisibility(R.id.btn_more_action, View.GONE);
        }


        // 文本
        if (!TextUtils.isEmpty(item.getWords())) {
            //

//            holder.setText(R.id.tv_content_view, item.getWords());
//            PostContentView view = holder.getView(R.id.tv_content_view);
//            view.setContentText(item.getWords());

            holder.setVisibility(R.id.tv_content_view, View.VISIBLE);
            final PostContentView contentView = holder.getView(R.id.tv_content_view);
            contentView.setContentText(item.getWords());
            int count = contentView.getLineCount();
            contentView.setMaxLines(15);
            if (count > 15) {
                holder.setVisibility(R.id.btn_all_text, View.VISIBLE);
                holder.setVisibility(R.id.btn_pick_up_text, View.GONE);
                holder.setOnClickListener(R.id.btn_all_text, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        contentView.setMaxLines(400);
                        holder.setVisibility(R.id.btn_all_text, View.GONE);
                        holder.setVisibility(R.id.btn_pick_up_text, View.VISIBLE);
                    }
                });
                holder.setOnClickListener(R.id.btn_pick_up_text, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        contentView.setMaxLines(15);
                        holder.setVisibility(R.id.btn_all_text, View.VISIBLE);
                        holder.setVisibility(R.id.btn_pick_up_text, View.GONE);
                    }
                });
            } else {
                holder.setVisibility(R.id.btn_all_text, View.GONE);
            }
            contentView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (listenler != null) {
                        listenler.onLongClickPost(item,position);
                    }
                    return true;
                }
            });

        } else {
            holder.setVisibility(R.id.tv_content_view, View.GONE);
            holder.setVisibility(R.id.btn_all_text, View.GONE);
            holder.setVisibility(R.id.btn_pick_up_text, View.GONE);
        }
        //图片
        if (viewType == PostsType.POSTS_TYPE_ONE.ordinal()) {
            setIndexOneImageView(holder,item,0);
        } else if (viewType == PostsType.POSTS_TYPE_TWO.ordinal()) {
            setIndexImageView(holder,item,0);
            setIndexImageView(holder,item,1);
        } else if (viewType == PostsType.POSTS_TYPE_THREE.ordinal()) {
            setIndexImageView(holder,item,0);
            setIndexImageView(holder,item,1);
            setIndexImageView(holder,item,2);
        } else if (viewType == PostsType.POSTS_TYPE_FOUR.ordinal()) {
            setIndexImageView(holder,item,0);
            setIndexImageView(holder,item,1);
            setIndexImageView(holder,item,2);
            setIndexImageView(holder,item,3);
        } else if (viewType == PostsType.POSTS_TYPE_FIVE.ordinal()) {
            setIndexImageView(holder,item,0);
            setIndexImageView(holder,item,1);
            setIndexImageView(holder,item,2);
            setIndexImageView(holder,item,3);
            setIndexImageView(holder,item,4);
            if (item.getAlbum().size() == 6) {
                setIndexImageView(holder,item,5);
                holder.setVisibility(R.id.iv_pic_six, View.VISIBLE);
            } else {
                holder.setVisibility(R.id.iv_pic_six, View.GONE);
            }
        } else if (viewType == PostsType.POSTS_TYPE_SIX.ordinal()) {
            setIndexImageView(holder,item,0);
            setIndexImageView(holder,item,1);
            setIndexImageView(holder,item,2);
            setIndexImageView(holder,item,3);
            setIndexImageView(holder,item,4);
            setIndexImageView(holder,item,5);
            setIndexImageView(holder,item,6);

            if (item.getAlbum().size() == 8) {
                setIndexImageView(holder,item,7);
                holder.setVisibility(R.id.iv_pic_eight, View.VISIBLE);
                holder.setVisibility(R.id.iv_pic_nine, View.GONE);
            } else if (item.getAlbum().size() == 9) {
                setIndexImageView(holder,item,7);
                setIndexImageView(holder,item,8);
                holder.setVisibility(R.id.iv_pic_nine, View.VISIBLE);
                holder.setVisibility(R.id.iv_pic_eight, View.VISIBLE);
            } else {
                holder.setVisibility(R.id.iv_pic_nine, View.GONE);
                holder.setVisibility(R.id.iv_pic_eight, View.GONE);
            }
        }

        //评论
        final ArrayList<Comment> comments = item.getComments();
        if (comments != null && comments.size() > 0) {
            holder.setVisibility(R.id.llv_comments_details, View.VISIBLE);
            final LinearListView linearListView = (LinearListView) holder.getView(R.id.llv_comments_details);
            linearListView.setAdapter(commentAdapter);//todo
            commentAdapter.replaceAll(comments);
            linearListView.setOnItemClickListener(new LinearListView.OnItemClickListener() {
                @Override
                public void onItemClick(LinearListView parent, View view, int position, long id) {
                    if (listenler != null) {
                        listenler.actionComment(item, position);
                    }
                }

                @Override
                public void onItemLongClick(LinearListView parent, View view, int position, long id) {
                    if (listenler != null) {
                        listenler.onLongClickComment(item, position);
                    }
                }
            });
        } else {
            holder.setVisibility(R.id.llv_comments_details, View.GONE);
        }

//        llv_comments_details


    }

    private void setIndexOneImageView(final SuperViewHolder holder, final PostsCard item, final int index){
        ImageView picView = holder.getView(viewIds[index]);
        GlideUitl.showImageViewShitWithThumb(item.getAlbumWithIndex(index) + "", picView);
        holder.setOnClickListener(viewIds[index], new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoView.builder()
                        .setPhotos(item.getAlbum())
                        .setCurrentItem(index)
                        .start(activity);
            }
        });
    }


    private void setIndexImageView(final SuperViewHolder holder, final PostsCard item, final int index){
        ImageView picView = holder.getView(viewIds[index]);
        GlideUitl.showImageViewWithThumb(item.getAlbumWithIndex(index) + "", picView);
        holder.setOnClickListener(viewIds[index], new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoView.builder()
                        .setPhotos(item.getAlbum())
                        .setCurrentItem(index)
                        .start(activity);
            }
        });
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public interface PracticalCommuActionListenler {
        void deletePosts(PostsCard item, int position);

        void sharePosts(PostsCard item);

        void commentPosts(PostsCard item, int position);

        void moreActionPosts(PostsCard item);

        void praisePosts(PostsCard item, int position);

        void unpraisePosts(PostsCard item, int position);

        void actionComment(PostsCard item, int commentPosition); //点击评论

        void onLongClickComment(PostsCard item, int commentPosition); //长按评论

        void clickAvatar(PostsCard item, int position); //点击头像

        void onLongClickPost(PostsCard item, int commentPosition); //长按帖子

    }

    public void setActionListenler(PracticalCommuActionListenler listenler) {
        this.listenler = listenler;
    }



    public void setCanShare(boolean canShare) {
        this.canShare = canShare;
    }

    public void setHasMoreAction(boolean hasMoreAction) {
        this.hasMoreAction = hasMoreAction;
    }

    public void setCanDelete(boolean canDelete) {
//        this.canDelete = canDelete;
    }

    public void setProhibited(boolean prohibited) {
        this.prohibited = prohibited;
    }


    public void setFriendCircle(boolean friendCircle) {
        isFriendCircle = friendCircle;
    }
}
